//
//  XMGPopoverPresentationController.m
//  弹出菜单
//
//  Created by x on 2017/3/22.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "XMGPopoverPresentationController.h"

@implementation XMGPopoverPresentationController


/**
 初始化方法 用于创建专场动画的对象

 @param presentedViewController 被展现的控制器
 @param presentingViewController 发起的控制器  可能是null
 @return 返回负责转产动画的对象
 */
- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
{
    self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
    if (self) {
        
        //NSLog(@"%@=========%@",presentedViewController,presentingViewController);
    }
    return self;
}

- (void)containerViewWillLayoutSubviews{
    [super containerViewWillLayoutSubviews];
    //1.修改弹出视图的大小
    //拿到被展现的视图 (也可以拿到被展现的控制器)
    self.presentedView.frame = self.popoverFrame;
    self.presentedView.layer.cornerRadius = 4;
    self.presentedView.layer.masksToBounds = YES;
    //2.给容易视图添加 蒙版视图
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = [UIColor clearColor];//[UIColor colorWithWhite:0.4 alpha:0.6];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
    [view addGestureRecognizer:tap];
    
    //把蒙版视图 View添加到容器视图中的最底层
    //拿到容器视图
    //这个方法是在某个View插入一个子视图 并且插在那个地方
    [self.containerView insertSubview:view atIndex:0];
}

- (void)tapGR:(UITapGestureRecognizer *)tap
{
    //点击蒙版视图 拿到当前展示的那个视图 然后dimiss
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}


@end
